package com.gtfrank.web.servlet.util;

import java.io.IOException;
import java.util.Properties;

/**
 * @ProjectName: jdbcourse
 * @ClassName: DBConfig
 * @PackageName:com.jdbc.util
 * @Description: 单列模式
 * @Author: frank
 * @Date: 2021/3/19 15:02
 * @Version v1.0
 */
public class DBConfig {
    private Properties properties = new Properties();
    private static DBConfig dbConfig  = null;
    private String path="dbconfig.properties";
    private DBConfig(){
        //读文件
        read();
    }

    private void read() {
        try {
            //这种方式如果上了服务器就有问题，在se环境下this DBConfig
            //如果上了服务器 tomcat  ee环境下this就是tomcat
            properties.load(DBConfig.class.getClassLoader().getResourceAsStream(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String getValue(String key){
        return properties.getProperty(key);
    }
    public synchronized static DBConfig getInstance(){
        if(dbConfig ==null){
            dbConfig = new DBConfig();
        }
        return dbConfig;
    }

    public static void main(String[] args) {
        System.out.println(DBConfig.getInstance().getValue("driverClassName"));
    }
}
